const app = getApp()
import { getSampleList, postDeleteOrder, postRemindDelivery, postConfirmReceipt } from '../../../../api/index'

Page({
  data: {
    status: '',
    currentPage: 1,
    pageSize: 15,
    isEmpty: false,
    list: [],
    refresher: false
  },
  async onRefresh () {
    await this.getList()
    this.setData({ refresher: false })
  },
  seeDetail ({ currentTarget: { dataset: { item } } }) {
    console.log(1111)
    wx.$nav.navigateTo(`/pages/account/order/detail/index?id=${item.id}`)
  },
  async delOrder ({ currentTarget: { dataset: { item } } }) {
    try {
      if (this.locking) return
      const { confirm } = await wx.$pro.showModal({ title: '确定要删除该订单吗？' })
      this.locking = true
      if (confirm) {
        await postDeleteOrder({ orderId: item.id })
        wx.showToast({ title: '删除成功！', icon: 'none' })
        this.getList()
      }
      this.locking = false
    } catch (err) {
      this.locking = false
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async remindDelivery ({ currentTarget: { dataset: { item } } }) {
    try {
      await postRemindDelivery({ orderId: item.id })
      await wx.$pro.showModal({ title: '已提醒商家发货' })
      this.getList()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async confirmDelivery ({ currentTarget: { dataset: { item } } }) {
    try {
      const { confirm } = await wx.$pro.showModal({ title: '确认收到货了吗？', content: '为保证你的售后权益，请收到商品确认无误后再确认收货' })
      if (confirm) {
        await postConfirmReceipt({ orderId: item.id })
        wx.showToast({ title: '已确认收货！', icon: 'none' })
        this.getList()
      }
      this.getList()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  changeTab ({ currentTarget: { dataset: { value } } }) {
    this.setData({ status: value })
    this.getList()
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      let query = {
        currentPage: this.data.currentPage,
        pageSize: this.data.pageSize
      }
      if (this.data.status) query.status = this.data.status
      const { ResultSet } = await getSampleList({ ...query })
      this.alloaded = !ResultSet.userData.data.length
      this.data.isEmpty = !ResultSet.userData.data.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.data.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.data.list.push(...ResultSet.userData.data)
        this.setData({
          list: this.data.list,
          currentPage: this.data.currentPage
        })
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async getList () {
    try {
      let query = {
        currentPage: 1,
        pageSize: this.data.pageSize
      }
      if (this.data.status) query.status = this.data.status
      wx.$pro.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getSampleList({ ...query })
      this.loading = false
      this.alloaded = false
      if (ResultSet.userData.data.length) this.data.currentPage = 2
      this.data.isEmpty = ResultSet.userData.data.length && ResultSet.userData.data.length < this.data.pageSize
      this.setData({
        list: ResultSet.userData.data,
        currentPage: this.data.currentPage,
        isEmpty: this.data.isEmpty
      })
      this.loading = false
      wx.$pro.hideLoading()
    } catch (err) {
      wx.$pro.hideLoading()
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  onLoad ({ status }) {
    this.setData({ status: status ? status : '' })
  },
  onShow () {
    this.getList()
  }
})